package com.yangjun.service.departmentCustomization;

import java.text.Collator;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yangjun.dao.mybatis.mapper.departmentCustomizationAnalysis.PreSalesSectionMapper;
import com.yangjun.dao.mybatis2.mapper.transactionAnalysis.SalesAnalysisMapper;
import com.yangjun.entity.common.CompareEntity;
import com.yangjun.entity.departmentCustomizationAnalysis.PreSalesSectionOverViewEntity;
import com.yangjun.utils.DvDateUtils;


public class DepartmentCustomizationCommonService {
	
	protected SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
	protected SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM");

	
	@Autowired
	protected PreSalesSectionMapper preSalesSectionMapper;
	@Autowired
	protected SalesAnalysisMapper salesAnalysisMapper;
	/**
	 * @Description 获取药师列表
	 * @return
	 * @author yangjun
	 * @Date 2017年3月15日 下午5:35:26
	 */
	protected List<String> queryStaffNameList(){
		return preSalesSectionMapper.queryStaffNameList();
	}
	/**
	 * @Description 获取重点产品列表
	 * @return
	 * @author yangjun
	 * @Date 2017年3月20日 下午5:46:25
	 */
	protected List<String> queryKeyProductList(){
		return preSalesSectionMapper.queryKeyProductList();
	}
	
	  
	/**
	 * @Description获取科室列表
	 * @return
	 * @author yangjun
	 * @Date 2017年3月10日 下午2:32:05
	 */
	protected List<String> querySectionNameList(){
		return preSalesSectionMapper.querySectionNameList();
	}
	/**
	 * @Description 获取渠道列表
	 * @return
	 * @author yangjun
	 * @Date 2017年3月10日 下午2:32:19
	 */
	protected List<String> queryChannelList(){
		List<String> channelList =  preSalesSectionMapper.queryChannelNameList();
		channelList.add(0, "全部");
		return channelList;
	}
	
	/**
	 * @Description 获取科室列表
	 * @return
	 * @author yangjun
	 * @Date 2017年3月10日 下午2:32:32
	 */
	@SuppressWarnings("unchecked")
	protected List<String> queryRoomList(){
		List<String> resultList = Lists.newArrayList();
		List<String> roomlList = salesAnalysisMapper.getCatnameByType("运营科室");
		
		@SuppressWarnings("rawtypes")
		Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
		String[] arr =roomlList.toArray(new String[roomlList.size()]);
		Arrays.sort(arr,cmp);
		
		resultList.add(0, "全部");
		resultList.addAll(Arrays.asList(arr));
		return resultList;
	}
	/**
	 * 
	 * @Description 查询平台列表
	 * @return
	 * @author yangjun
	 * @Date 2017年3月16日 上午10:11:34
	 */
	protected List<String> querySourceNameList(){
		List<String> sourceList = preSalesSectionMapper.querySourceNameList();
		sourceList.add(0, "全部");
		return sourceList;
	}
	
	
	/**
	 * @Description 周维度转化
	 * @param entityList
	 * @param tmpDate
	 * @return
	 * @author yangjun
	 * @Date 2017年3月10日 下午2:41:01
	 */
	protected List<PreSalesSectionOverViewEntity> setDayOfWeek(List<PreSalesSectionOverViewEntity> entityList,Date tmpDate){
		Calendar calendar = Calendar.getInstance();
		try {
			for(PreSalesSectionOverViewEntity entity : entityList){
					Date date = sdfDay.parse(entity.getDataDate());
					calendar.setTime(date);
					if(date.after(tmpDate)){
						entity.setDataDate("本周");
					}else{
						entity.setDataDate("上周");
					}
					int dayOfWeek= calendar.get(Calendar.DAY_OF_WEEK)-1;
					if(dayOfWeek==1){
						entity.setDegreeDate("周一");
					}else if(dayOfWeek==2){
						entity.setDegreeDate("周二");
					}else if(dayOfWeek==3){
						entity.setDegreeDate("周三");
					}else if(dayOfWeek==4){
						entity.setDegreeDate("周四");
					}else if(dayOfWeek==5){
						entity.setDegreeDate("周五");
					}else if(dayOfWeek==6){
						entity.setDegreeDate("周六");
					}else {
						entity.setDegreeDate("周日");
					}
				}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return entityList;
	}
	/**
	 * @Description 查询对比折线图数据
	 * @param xAxisArr
	 * @param PreSalesSectionOverViewEntityList
	 * @param dateArr
	 * @param nameArr
	 * @return
	 * @author yangjun
	 * @Date 2017年3月10日 下午2:40:19
	 */
	protected Map<String,Object> queryLineChartData(String[] xAxisArr,
			List<PreSalesSectionOverViewEntity> PreSalesSectionOverViewEntityList,String[] dateArr,String[] nameArr,int index){
		//用来返回结果的结果集
		Map<String,Object> resultMap = Maps.newHashMap();
		//用来存放一天每个小时的数据（或其他时间维度）
		Map<String,PreSalesSectionOverViewEntity> entityMap = null;
		//用来组装每天每个小时的数据（或其他时间维度）
		Map<String,Map<String,PreSalesSectionOverViewEntity>> dateMap = Maps.newHashMap();
		//用来存放返回的对象名
		Map<String,String> nameMap = Maps.newHashMap();
		Map<String,String> dimensionMap = Maps.newHashMap();
		dimensionMap.put("salesOrderAmount", "销售额");
		dimensionMap.put("orderCnt", "订单数");
		dimensionMap.put("adviceCnt", "咨询量");
		dimensionMap.put("salePct", "客单价");
		dimensionMap.put("validAdviceCnt", "有效咨询量");
		dimensionMap.put("nopayOrderAmount", "未付款销售额");
		dimensionMap.put("nopayOrderCnt", "未付款订单数");
		dimensionMap.put("allOrderCnt", "6s订单数");
		dimensionMap.put("allOrderAmount", "6s销售额");
		dimensionMap.put("allsalePct", "6s客单价");

		if(PreSalesSectionOverViewEntityList.size()>0){
			//遍历集合提取时间作为key，实体作为value,封装不同日期的Map集合
			for(PreSalesSectionOverViewEntity entity : PreSalesSectionOverViewEntityList){
				for(int i=0,len=dateArr.length;i<len;i++){
					//存放返回的对象名
					nameMap.put(dateArr[i], nameArr[i]);
					if(entity.getDataDate().equals(dateArr[i])){
						entityMap = dateMap.get(entity.getDataDate());
						if(entityMap==null){
							entityMap = Maps.newHashMap();
						}
						entityMap.put(entity.getDegreeDate(), entity);
						dateMap.put(entity.getDataDate(), entityMap);
					}
				}
			}
			Map<String,CompareEntity> resultDataMap = Maps.newHashMap();
	
			//遍历当前日期以及对比日期
			for(Map.Entry<String,Map<String,PreSalesSectionOverViewEntity>> entryDateMap: dateMap.entrySet()){
				
				String date = nameMap.get(entryDateMap.getKey());
				Map<String,PreSalesSectionOverViewEntity> itemMap= entryDateMap.getValue();
				
				String[] salesOrderAmountArr = new String[xAxisArr.length];
				String[] orderCntArr = new String[xAxisArr.length];
				String[] adviceCntArr = new String[xAxisArr.length];
				String[] salePctArr = new String[xAxisArr.length];
				String[] validAdviceCntArr = new String[xAxisArr.length];
				String[] nopayOrderAmountArr = new String[xAxisArr.length];
				String[] nopayOrderCntArr = new String[xAxisArr.length];
				String[] allOrderCntArr = new String[xAxisArr.length];
				String[] allOrderAmountArr = new String[xAxisArr.length];
				String[] allsalePctArr = new String[xAxisArr.length];

				

				//遍历时间数组获取对应的时间实体，空数据给默认值
				for(int i=0,len=xAxisArr.length;i<len;i++ ){
					salesOrderAmountArr[i]=salePctArr[i]=orderCntArr[i]=adviceCntArr[i]=validAdviceCntArr[i]=nopayOrderAmountArr[i]
							=nopayOrderCntArr[i]=allOrderCntArr[i]=allOrderAmountArr[i]=allsalePctArr[i]="0";
					PreSalesSectionOverViewEntity hourEntity = itemMap.get(xAxisArr[i]);
					if(hourEntity!=null){
						salesOrderAmountArr[i] = hourEntity.getSalesOrderAmount().equals(0f)?"0":String.valueOf(Math.round(hourEntity.getSalesOrderAmount()));
						orderCntArr[i]  = String.valueOf(hourEntity.getOrderCnt());
						adviceCntArr[i] = String.valueOf(hourEntity.getAdviceCnt());
						salePctArr[i] = hourEntity.getSalePct().equals(0f)?"0":String.valueOf(Math.round(hourEntity.getSalePct()));
						validAdviceCntArr[i] = String.valueOf(hourEntity.getValidAdviceCnt());
						nopayOrderAmountArr[i] = hourEntity.getNopayOrderAmount().equals(0f)?"0":String.valueOf(Math.round(hourEntity.getNopayOrderAmount()));
						nopayOrderCntArr[i]=String.valueOf(hourEntity.getNopayOrderCnt());
						
						allOrderCntArr[i]  = String.valueOf(Integer.valueOf(orderCntArr[i])+Integer.valueOf(nopayOrderCntArr[i]));
						allOrderAmountArr[i] = String.valueOf(Integer.valueOf(salesOrderAmountArr[i])+Integer.valueOf(nopayOrderAmountArr[i]));
						allsalePctArr[i] = String.valueOf(Math.round(Float.valueOf(allOrderAmountArr[i])/Float.valueOf(allOrderCntArr[i])));

					}
				}
				addItemToResultMap(date,dimensionMap.get("salesOrderAmount"),"salesOrderAmount",salesOrderAmountArr,resultDataMap,index);
				addItemToResultMap(date,dimensionMap.get("orderCnt"),"orderCnt",orderCntArr,resultDataMap,index);
				addItemToResultMap(date,dimensionMap.get("adviceCnt"),"adviceCnt",adviceCntArr,resultDataMap,index);
				addItemToResultMap(date,dimensionMap.get("salePct"),"salePct",salePctArr,resultDataMap,index);
				addItemToResultMap(date,dimensionMap.get("validAdviceCnt"),"validAdviceCnt",validAdviceCntArr,resultDataMap,index);
				addItemToResultMap(date,dimensionMap.get("nopayOrderAmount"),"nopayOrderAmount",nopayOrderAmountArr,resultDataMap,index);
				addItemToResultMap(date,dimensionMap.get("nopayOrderCnt"),"nopayOrderCnt",nopayOrderCntArr,resultDataMap,index);
				addItemToResultMap(date,dimensionMap.get("allOrderCnt"),"allOrderCnt",allOrderCntArr,resultDataMap,index);
				addItemToResultMap(date,dimensionMap.get("allOrderAmount"),"allOrderAmount",allOrderAmountArr,resultDataMap,index);
				addItemToResultMap(date,dimensionMap.get("allsalePct"),"allsalePct",allsalePctArr,resultDataMap,index);

			}
			for(Map.Entry<String, CompareEntity> entry : resultDataMap.entrySet()){
				String name = entry.getKey();
				CompareEntity entity = entry.getValue();
				int contrastDataSum=0;
				int currentDataSum=0;
				String[] contrastDataArr = entity.getContrastDataArr();
				String[] currentDataArr = entity.getCurrentDataArr();
				String[] contrastDataSumArr = new String[contrastDataArr.length];
				String[] currentDataSumArr = new String[currentDataArr.length];
				if("salePct".equals(name)){
					String[] currentOrderDataArr = resultDataMap.get("orderCnt").getCurrentDataArr();
					String[] contrastOrderDataArr = resultDataMap.get("orderCnt").getContrastDataArr();
					String[] currentOrderAmountDataArr = resultDataMap.get("salesOrderAmount").getCurrentDataArr();
					String[] contrastOrderAmountDataArr = resultDataMap.get("salesOrderAmount").getContrastDataArr();
					for(int i=0,len=currentDataSumArr.length;i<len;i++){
						currentDataSumArr[i]=Fribonacci(i,currentOrderDataArr)==0?"0":String.valueOf(Math.round((float)Fribonacci(i,currentOrderAmountDataArr)/Fribonacci(i,currentOrderDataArr)));
					}
					for(int i=0,len=contrastDataSumArr.length;i<len;i++){
						contrastDataSumArr[i]=Fribonacci(i,contrastOrderDataArr)==0?"0":String.valueOf(Math.round((float)Fribonacci(i,contrastOrderAmountDataArr)/Fribonacci(i,contrastOrderDataArr)));
					}
				}else if("allsalePct".equals(name)){
					String[] currentAllOrderDataArr = resultDataMap.get("allOrderCnt").getCurrentDataArr();
					String[] contrastAllOrderDataArr = resultDataMap.get("allOrderCnt").getContrastDataArr();
					String[] currentAllOrderAmountDataArr = resultDataMap.get("allOrderAmount").getCurrentDataArr();
					String[] contrastAllOrderAmountDataArr = resultDataMap.get("allOrderAmount").getContrastDataArr();
					for(int i=0,len=currentDataSumArr.length;i<len;i++){
						currentDataSumArr[i]=Fribonacci(i,currentAllOrderDataArr)==0?"0":String.valueOf(Math.round((float)Fribonacci(i,currentAllOrderAmountDataArr)/Fribonacci(i,currentAllOrderDataArr)));
					}
					for(int i=0,len=contrastDataSumArr.length;i<len;i++){
						contrastDataSumArr[i]=Fribonacci(i,contrastAllOrderDataArr)==0?"0":String.valueOf(Math.round((float)Fribonacci(i,contrastAllOrderAmountDataArr)/Fribonacci(i,contrastAllOrderDataArr)));
					}
				}else{
					for(int i=0,len=contrastDataArr.length;i<len;i++){
						contrastDataSum+=Integer.valueOf(contrastDataArr[i]);
						contrastDataSumArr[i]=String.valueOf(contrastDataSum);
					}
					for(int i=0,len=currentDataArr.length;i<len;i++){
						currentDataSum+=Integer.valueOf(currentDataArr[i]);
						currentDataSumArr[i]=String.valueOf(currentDataSum);
					}
				}
				entity.setContrastDataSumArr(contrastDataSumArr);
				entity.setCurrentDataSumArr(currentDataSumArr);
			}
			resultMap.putAll(resultDataMap);
		}else{
			for(Map.Entry<String,String> entry : dimensionMap.entrySet()){
				CompareEntity compareEntity = new CompareEntity();
				compareEntity.setContrastDataArr(new String[]{});
				compareEntity.setCurrentDataArr(new String[]{});
				compareEntity.setName(entry.getValue());
				resultMap.put(entry.getKey(), compareEntity);
			}
		}
		//构建不同维度的数组
		resultMap.put("xAxis",xAxisArr);
		resultMap.put("legendName",dateArr);
		return resultMap;
	}
	  public static int Fribonacci(int n,String[] arr){
           if(n==0)
               return Integer.valueOf(arr[0]);
           else
              return Integer.valueOf(arr[n])+Fribonacci(n-1,arr);
    }
	/**
	 * @Description 组装折线图数据
	 * @param date
	 * @param legendName
	 * @param deminsionName
	 * @param itemArr
	 * @param resultDataMap
	 * @return
	 * @author yangjun
	 * @Date 2017年3月10日 下午2:40:42
	 */
	protected Map<String,CompareEntity> addItemToResultMap(String date,String legendName,String deminsionName,String[] itemArr,Map<String,CompareEntity> resultDataMap,int index){
		CompareEntity compareEntity = resultDataMap.get(deminsionName);
		if(compareEntity==null){
			compareEntity = new CompareEntity();
			compareEntity.setName(legendName);
		}
		if("currentData".equals(date)){
			compareEntity.setCurrentDataArr(Arrays.copyOfRange(itemArr, 0, index));
		}else if("contrastData".equals(date)){
			compareEntity.setContrastDataArr(itemArr);
		}
		resultDataMap.put(deminsionName, compareEntity);
		return resultDataMap;
	}
	
	/**
	 * @Description 查询表格数据
	 * @param dataList
	 * @param degreeDateArr
	 * @return
	 * @author yangjun
	 * @Date 2017年3月10日 下午2:38:52
	 */
	protected Map<String,List<String[]>> queryTableData(List<PreSalesSectionOverViewEntity> dataList,String[] degreeDateArr,String deminsion,String dateSortFlag){
		//存放时间维度如天的数据
		Map<String,Map<String,Map<String,PreSalesSectionOverViewEntity>>> dateDeminsionDegreeDateMap = Maps.newTreeMap(new dateMapComparator(dateSortFlag));
		//存放时间维度如每一天每个维度的数据
		Map<String,Map<String,PreSalesSectionOverViewEntity>> deminsionDegreeDateMapMap = null;
		//存放时间维度如每一天每个维度每小时的数据
		Map<String,PreSalesSectionOverViewEntity> degreeDateMap = null;
		//返回的维度集合List
		Map<String,List<String[]>> resultMap = Maps.newHashMap();
		//初始值
		String initialValue = "0";
		//组装成<时间维度如天<工具<小时:数据>>>的格式
		if(dataList.size()>0){
			for(PreSalesSectionOverViewEntity entity : dataList){
				String date = entity.getDataDate();
				String deminsionName = null;
				if("channel".equals(deminsion)){
					deminsionName= entity.getChannel();
				}else if("adviceKeyword".equals(deminsion)){
					deminsionName= entity.getAdviceKeyword();
				}else if("staffName".equals(deminsion)){
					deminsionName= entity.getStaffName();
				}
				String degreeDate = entity.getDegreeDate();
				deminsionDegreeDateMapMap = dateDeminsionDegreeDateMap.get(date);
				if(deminsionDegreeDateMapMap==null){
					deminsionDegreeDateMapMap = Maps.newHashMap();
				}
				degreeDateMap= deminsionDegreeDateMapMap.get(deminsionName);
				if(degreeDateMap==null){
					degreeDateMap = Maps.newHashMap();
				}
				degreeDateMap.put(degreeDate, entity);
				deminsionDegreeDateMapMap.put(deminsionName, degreeDateMap);
				dateDeminsionDegreeDateMap.put(date, deminsionDegreeDateMapMap);
			}
			//循环每一时间维度如天
			for(Map.Entry<String,Map<String,Map<String,PreSalesSectionOverViewEntity>>> dateChannelHourEntry : dateDeminsionDegreeDateMap.entrySet()){
				//获取时间维度如天
				String date = dateChannelHourEntry.getKey();
				Map<String,Map<String,PreSalesSectionOverViewEntity>> channelHourEntry = dateChannelHourEntry.getValue();
				//循环每个工具
				for(Map.Entry<String,Map<String,PreSalesSectionOverViewEntity>> hourEntry :channelHourEntry.entrySet()){
					//获取工具名
					String channel = hourEntry.getKey();
					//初始化每个工具不同维度的数组
					String[] titleDegreeDateArr = degreeDateArr;
					String[] salesOrderAmountArr = new String[titleDegreeDateArr.length+2];
					String[] orderCntArr = new String[titleDegreeDateArr.length+2];
					String[] adviceCntArr = new String[titleDegreeDateArr.length+2];
					String[] salePctArr = new String[titleDegreeDateArr.length+2];
					String[] validAdviceCntArr = new String[titleDegreeDateArr.length+2];
					String[] nopayOrderAmountArr = new String[titleDegreeDateArr.length+2];
					String[] recommendCntArr = new String[titleDegreeDateArr.length+2];
					String[] recommendRateArr = new String[titleDegreeDateArr.length+2];
					//获取时间维度如每天每个工具某些小时的数据，可能存在某个小时没数据
					Map<String,PreSalesSectionOverViewEntity> entryHourMap = hourEntry.getValue();
					//遍历默认的24小时填充数据
					for(int i=0,len=titleDegreeDateArr.length;i<len;i++){
						PreSalesSectionOverViewEntity preSalesSectionOverViewEntity = entryHourMap.get(titleDegreeDateArr[i]);
						//如果遍历到当前的小时有数据就填充数据否则赋值为0
						if(preSalesSectionOverViewEntity!=null){
							salesOrderAmountArr[i+2]=preSalesSectionOverViewEntity.getSalesOrderAmount()==null?initialValue:String.valueOf(Math.round(preSalesSectionOverViewEntity.getSalesOrderAmount()));
							orderCntArr[i+2]  = preSalesSectionOverViewEntity.getOrderCnt()==null?initialValue:String.valueOf(preSalesSectionOverViewEntity.getOrderCnt());
							adviceCntArr[i+2] = preSalesSectionOverViewEntity.getAdviceCnt()==null?initialValue:String.valueOf(preSalesSectionOverViewEntity.getAdviceCnt());
							salePctArr[i+2] = preSalesSectionOverViewEntity.getSalePct()==null?initialValue:String.valueOf(Math.round(preSalesSectionOverViewEntity.getSalePct()));
							validAdviceCntArr[i+2] = preSalesSectionOverViewEntity.getValidAdviceCnt()==null?initialValue:String.valueOf(preSalesSectionOverViewEntity.getValidAdviceCnt());
							nopayOrderAmountArr[i+2] = preSalesSectionOverViewEntity.getNopayOrderAmount()==null?initialValue:String.valueOf(Math.round(preSalesSectionOverViewEntity.getNopayOrderAmount()));
							recommendCntArr[i+2] = preSalesSectionOverViewEntity.getRecommendCnt()==null?initialValue:String.valueOf(preSalesSectionOverViewEntity.getRecommendCnt());
							recommendRateArr[i+2] = preSalesSectionOverViewEntity.getRecommendRate()==null?initialValue:String.valueOf(preSalesSectionOverViewEntity.getRecommendRate());
						}else{
							salesOrderAmountArr[i+2]=orderCntArr[i+2]=adviceCntArr[i+2]=salePctArr[i+2]=validAdviceCntArr[i+2]=nopayOrderAmountArr[i+2]=recommendCntArr[i+2]=recommendRateArr[i+2]=initialValue;
						}
						//默认第一列是日期，第二列是工具名
						salesOrderAmountArr[0]=orderCntArr[0]=adviceCntArr[0]=salePctArr[0]=validAdviceCntArr[0]=nopayOrderAmountArr[0]=recommendCntArr[0]=recommendRateArr[0]=date;
						salesOrderAmountArr[1]=orderCntArr[1]=adviceCntArr[1]=salePctArr[1]=validAdviceCntArr[1]=nopayOrderAmountArr[1]=recommendCntArr[1]=recommendRateArr[1]=channel;
					}
					//根据不同维度填充数据
					putTableItemByDeminsion("salesOrderAmount",salesOrderAmountArr,resultMap);
					putTableItemByDeminsion("orderCnt",orderCntArr,resultMap);
					putTableItemByDeminsion("adviceCnt",adviceCntArr,resultMap);
					putTableItemByDeminsion("salePct",salePctArr,resultMap);
					putTableItemByDeminsion("validAdviceCnt",validAdviceCntArr,resultMap);
					putTableItemByDeminsion("nopayOrderAmount",nopayOrderAmountArr,resultMap);
					putTableItemByDeminsion("recommendCnt",recommendCntArr,resultMap);
					putTableItemByDeminsion("recommendRate",recommendRateArr,resultMap);
				}
			}
		}else{
			List<String[]> nullList = Lists.newArrayList();
			resultMap.put("salesOrderAmount",nullList);
			resultMap.put("orderCnt",nullList);
			resultMap.put("adviceCnt",nullList);
			resultMap.put("salePct",nullList);
			resultMap.put("validAdviceCnt",nullList);
			resultMap.put("nopayOrderAmount",nullList);
			resultMap.put("recommendCnt",nullList);
			resultMap.put("recommendRate",nullList);
		}
		return resultMap;
	}
	
	protected Map<String,List<String[]>> queryTableData(List<PreSalesSectionOverViewEntity> dataList,String[] degreeDateArr,String dateSortFlag){
		//返回的维度集合List
		Map<String,List<String[]>> resultMap = Maps.newHashMap();
		Map<String,Map<String,PreSalesSectionOverViewEntity>> sortDateMap = Maps.newTreeMap(new dateMapComparator(dateSortFlag));
		Map<String,Map<String,PreSalesSectionOverViewEntity>> dateMap= Maps.newHashMap();
		Map<String,PreSalesSectionOverViewEntity> degreeDateMap = null;
		//初始值
		String initialValue = "0";
		if(dataList.size()>0){
			for(PreSalesSectionOverViewEntity entity : dataList){
				String dataDate = entity.getDataDate();
				String degreeDate = entity.getDegreeDate();
				degreeDateMap = dateMap.get(dataDate);
				if(degreeDateMap==null){
					degreeDateMap = Maps.newHashMap();
				}
				degreeDateMap.put(degreeDate, entity);
				dateMap.put(dataDate, degreeDateMap);
			}
			sortDateMap.putAll(dateMap);
			for(Map.Entry<String,Map<String,PreSalesSectionOverViewEntity>> dateMapEntry :sortDateMap.entrySet() ){
				String date = dateMapEntry.getKey();
				String[] titleDegreeDateArr = degreeDateArr;
				String[] salesOrderAmountArr = new String[titleDegreeDateArr.length+2];
				String[] orderCntArr = new String[titleDegreeDateArr.length+2];
				String[] adviceCntArr = new String[titleDegreeDateArr.length+2];
				String[] salePctArr = new String[titleDegreeDateArr.length+2];
				String[] validAdviceCntArr = new String[titleDegreeDateArr.length+2];
				String[] nopayOrderAmountArr = new String[titleDegreeDateArr.length+2];
				String[] nopayOrderCntArr = new String[titleDegreeDateArr.length+2];
				
				String[] allOrderCntArr = new String[titleDegreeDateArr.length+2];
				String[] allOrderAmountArr = new String[titleDegreeDateArr.length+2];
				String[] allsalePctArr = new String[titleDegreeDateArr.length+2];


				int orderCntSum,adviceCntSum,validAdviceCntSum,nopayOrderCntSum,allOrderCntSum;
				float salesOrderAmountSum,nopayOrderAmountSum,allOrderAmountSum;
				orderCntSum=adviceCntSum=validAdviceCntSum=nopayOrderCntSum=allOrderCntSum=0;
				salesOrderAmountSum=nopayOrderAmountSum=allOrderAmountSum=0f;
				
				Map<String,PreSalesSectionOverViewEntity> degreeDateMapEntry = dateMapEntry.getValue();
				for(int i=0,len=titleDegreeDateArr.length;i<len;i++){
					PreSalesSectionOverViewEntity preSalesSectionOverViewEntity = degreeDateMapEntry.get(titleDegreeDateArr[i]);
					if(preSalesSectionOverViewEntity!=null){
						salesOrderAmountSum+=preSalesSectionOverViewEntity.getSalesOrderAmount();
						orderCntSum+=preSalesSectionOverViewEntity.getOrderCnt();
						adviceCntSum+=preSalesSectionOverViewEntity.getAdviceCnt();
						validAdviceCntSum+=preSalesSectionOverViewEntity.getValidAdviceCnt();
						nopayOrderAmountSum+=preSalesSectionOverViewEntity.getNopayOrderAmount();
						nopayOrderCntSum+=preSalesSectionOverViewEntity.getNopayOrderCnt();
						allOrderCntSum+=preSalesSectionOverViewEntity.getOrderCnt()+preSalesSectionOverViewEntity.getNopayOrderCnt();
						allOrderAmountSum+=preSalesSectionOverViewEntity.getSalesOrderAmount()+preSalesSectionOverViewEntity.getNopayOrderAmount();
						
						salesOrderAmountArr[i+2]=preSalesSectionOverViewEntity.getSalesOrderAmount()==null?initialValue:String.valueOf(Math.round(preSalesSectionOverViewEntity.getSalesOrderAmount()));
						orderCntArr[i+2]  = preSalesSectionOverViewEntity.getOrderCnt()==null?initialValue:String.valueOf(preSalesSectionOverViewEntity.getOrderCnt());
						adviceCntArr[i+2] = preSalesSectionOverViewEntity.getAdviceCnt()==null?initialValue:String.valueOf(preSalesSectionOverViewEntity.getAdviceCnt());
						salePctArr[i+2] = preSalesSectionOverViewEntity.getSalePct()==null?initialValue:String.valueOf(Math.round(preSalesSectionOverViewEntity.getSalePct()));
						validAdviceCntArr[i+2] = preSalesSectionOverViewEntity.getValidAdviceCnt()==null?initialValue:String.valueOf(preSalesSectionOverViewEntity.getValidAdviceCnt());
						nopayOrderAmountArr[i+2] = preSalesSectionOverViewEntity.getNopayOrderAmount()==null?initialValue:String.valueOf(Math.round(preSalesSectionOverViewEntity.getNopayOrderAmount()));
						nopayOrderCntArr[i+2] = preSalesSectionOverViewEntity.getNopayOrderCnt()==null?initialValue:String.valueOf(preSalesSectionOverViewEntity.getNopayOrderCnt());
					
						allOrderCntArr[i+2]  = String.valueOf(Integer.valueOf(orderCntArr[i+2])+Integer.valueOf(nopayOrderCntArr[i+2]));
						allOrderAmountArr[i+2]=String.valueOf(Integer.valueOf(salesOrderAmountArr[i+2])+Integer.valueOf(nopayOrderAmountArr[i+2]));
						allsalePctArr[i+2] = String.valueOf(Math.round(Float.valueOf(allOrderAmountArr[i+2])/Float.valueOf(allOrderCntArr[i+2])));
					}else{
						salesOrderAmountArr[i+2]=orderCntArr[i+2]=adviceCntArr[i+2]=salePctArr[i+2]=
						validAdviceCntArr[i+2]=nopayOrderAmountArr[i+2]=nopayOrderCntArr[i+2]=
						allOrderCntArr[i+2]=allOrderAmountArr[i+2]=allsalePctArr[i+2]=initialValue;
					}
					//默认第一列是日期，第二列是工具名
					salesOrderAmountArr[0]=orderCntArr[0]=adviceCntArr[0]=salePctArr[0]=validAdviceCntArr[0]=nopayOrderAmountArr[0]
					=nopayOrderCntArr[0]=allOrderCntArr[0]=allOrderAmountArr[0]=allsalePctArr[0]=date;
					salesOrderAmountArr[1]=String.valueOf(Math.round(salesOrderAmountSum));
					orderCntArr[1]=String.valueOf(orderCntSum);
					salePctArr[1]=String.valueOf(Math.round(salesOrderAmountSum/orderCntSum));
					adviceCntArr[1]=String.valueOf(adviceCntSum);
					validAdviceCntArr[1]=String.valueOf(validAdviceCntSum);
					nopayOrderAmountArr[1]=String.valueOf(Math.round(nopayOrderAmountSum));
					nopayOrderCntArr[1]=String.valueOf(nopayOrderCntSum);
					
					allOrderCntArr[1]=String.valueOf(allOrderCntSum);
					allOrderAmountArr[1]=String.valueOf(allOrderAmountSum);
					allsalePctArr[1]=String.valueOf(Math.round(allOrderAmountSum/allOrderCntSum));

				}
				//根据不同维度填充数据
				putTableItemByDeminsion("salesOrderAmount",salesOrderAmountArr,resultMap);
				putTableItemByDeminsion("orderCnt",orderCntArr,resultMap);
				putTableItemByDeminsion("adviceCnt",adviceCntArr,resultMap);
				putTableItemByDeminsion("salePct",salePctArr,resultMap);
				putTableItemByDeminsion("validAdviceCnt",validAdviceCntArr,resultMap);
				putTableItemByDeminsion("nopayOrderAmount",nopayOrderAmountArr,resultMap);
				putTableItemByDeminsion("nopayOrderCnt",nopayOrderCntArr,resultMap);
				
				putTableItemByDeminsion("allOrderCnt",allOrderCntArr,resultMap);
				putTableItemByDeminsion("allOrderAmount",allOrderAmountArr,resultMap);
				putTableItemByDeminsion("allsalePct",allsalePctArr,resultMap);
			}
		}else{
			List<String[]> nullList = Lists.newArrayList();
			resultMap.put("salesOrderAmount",nullList);
			resultMap.put("orderCnt",nullList);
			resultMap.put("adviceCnt",nullList);
			resultMap.put("salePct",nullList);
			resultMap.put("validAdviceCnt",nullList);
			resultMap.put("nopayOrderAmount",nullList);
			resultMap.put("recommendCnt",nullList);
			resultMap.put("recommendRate",nullList);
			resultMap.put("nopayOrderCnt",nullList);
			resultMap.put("allOrderCnt",nullList);
			resultMap.put("allOrderAmount",nullList);
			resultMap.put("allsalePct",nullList);
		}
		return resultMap;
	}
	/**
	 * @Description 组装表格数据
	 * @param deminsionName
	 * @param itemArr
	 * @param resultMap
	 * @return
	 * @author yangjun
	 * @Date 2017年3月10日 下午2:39:11
	 */
	protected Map<String,List<String[]>> putTableItemByDeminsion(String deminsionName,String[] itemArr,Map<String,List<String[]>> resultMap){
		List<String[]> deminsionList =  resultMap.get(deminsionName);
		float sum=0f;
		if(deminsionList==null){
			deminsionList = Lists.newArrayList();
		}
		for(int i=0,len=itemArr.length;i<len;i++){
			if(i>1){
				float value = Float.valueOf(itemArr[i]);
				sum+=value;
				if(value==0f){
					itemArr[i]="0";
				}
			}
		}
		//如一天都没有数据的不返回
		if(sum!=0f){
			deminsionList.add(itemArr);
		}
		resultMap.put(deminsionName, deminsionList);
		return resultMap;
		
	}
	
	//自定义比较器
	class FloatComparatorSortList implements Comparator<Map<String,Object>>{
		private String deminsion ;
		public FloatComparatorSortList(String deminsion){
			this.deminsion = deminsion;
		}
		@Override
		public int compare(Map<String, Object> map1, Map<String, Object> map2) {
			Float value1 = Float.valueOf(map1.get(deminsion).toString());
			Float value2 = Float.valueOf(map2.get(deminsion).toString());
			if(value1>=value2){
				return -1;
			}else{
				return 1;
			}
		}
	}
	class dateMapComparator implements Comparator<String>{
		private String deminsion ;
		public dateMapComparator(String deminsion){
			this.deminsion = deminsion;
		}
	    @Override
	    public int compare(String date1, String date2) {
	    	String pattern = null;
	    	if("day".equals(deminsion)){
	    		pattern="yyyy-MM-dd";
	    	}else if("month".equals(deminsion)){
	    		pattern="yyyy-MM";
	    	}else if("year".equals(deminsion)){
	    		pattern="yyyy";
	    	}
	    	SimpleDateFormat df = new SimpleDateFormat(pattern);
			try {
				Date d1 = df.parse(date1);
				Date d2 = df.parse(date2);
				if(d1.getTime()>d2.getTime()){
					return -1;
				}else if(d1.getTime()<d2.getTime()){
					return 1;
				}else{
					return 0;
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return 0;
	    }
	}
	protected List<Map<String,Object>> rankPreSalesSectionOverViewEntityList(List<PreSalesSectionOverViewEntity> preSalesSectionOverViewEntityList){
		List<Map<String,Object>> rankList = Lists.newArrayList();
		if(preSalesSectionOverViewEntityList.size()>0){
			for(PreSalesSectionOverViewEntity preSalesSectionOverViewEntity : preSalesSectionOverViewEntityList){
				String rankName = preSalesSectionOverViewEntity.getRankName()==null?null:preSalesSectionOverViewEntity.getRankName();
				Object rankValue = preSalesSectionOverViewEntity.getRankValue()==null?null:preSalesSectionOverViewEntity.getRankValue();
				Integer barValue = preSalesSectionOverViewEntity.getBarValue()==null?null:preSalesSectionOverViewEntity.getBarValue();
				Map<String,Object> rankItemMap = Maps.newHashMap();
				rankItemMap.put("name", rankName);
				rankItemMap.put("value", rankValue);
				rankItemMap.put("bar", barValue);
				rankList.add(rankItemMap);
			}
		}
		return rankList;
	}
	protected void sortRankPreSalesSectionOverViewEntityList(List<Map<String,Object>> rankList,Comparator<Map<String,Object>> comparator){
		Collections.sort(rankList,comparator);
	}
	
	protected int arrIsExistNull(int index,Object[] dataArr){
		int sum=0;
		for(int i=index,len=dataArr.length;i<len;i++){
			String numStr = dataArr[i].toString();
			if(numStr.contains(".")){
				if(Float.valueOf(numStr).equals(0f)){
					sum+=0;
					break;
				}
			}else{
				sum+=Integer.valueOf(numStr);
			}
		}
		return sum;
	}
}
